package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSON;
import com.ruoyi.purchase.domain.PoFirm;
import com.ruoyi.purchase.service.PoFirmService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import com.ruoyi.system.service.SysCityService;
import com.ruoyi.system.service.SysProvinceService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 厂商管理 Controller
 */
@Controller
@RequestMapping("/firm/manage")
public class FirmManageController extends BaseController {
    private String prefix="firm/manage/";

    @Autowired
    private PoFirmService poFirmService;
    @Autowired
    private SysCityService sysCityService;
    @Autowired
    private SysProvinceService sysProvinceService;

    @RequiresPermissions("firm:manage:view")
    @RequestMapping()
    public String firmManage(Model model){
        List<SysProvince> poFirmList= sysProvinceService.list();
        model.addAttribute("poFirmList",poFirmList);
        return prefix+"firmManage";
    }

    @RequestMapping("/city/{pid}")
    @ResponseBody
    public List<SysCity> city(@PathVariable("pid") Integer pId){
        List<SysCity> cityList= sysCityService.findCityList(pId);
        return cityList;
    }

    @RequiresPermissions("firm:manage:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@Validated PoFirm poFirm){
        startPage();
        List<PoFirm> firmList= poFirmService.getFirmList(poFirm);
        return getDataTable(firmList);
    }

    @RequestMapping("/add")
    public String add(Model model){
        List<SysProvince> poFirmList= sysProvinceService.list();
        model.addAttribute("poFirmList",poFirmList);
        model.addAttribute("cUser",getSysUser());
        return prefix+"add";
    }

    @RequiresPermissions("firm:manage:add")
    @Log(title = "商品管理",businessType = BusinessType.INSERT)
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResult addSave(PoFirm poFirm){
        return toAjax(poFirmService.save(poFirm));
    }

    @RequestMapping("/edit/{firmId}")
    public String edit(@PathVariable("firmId") Integer firmId,Model model){
        PoFirm poFirm = poFirmService.getById(firmId);
        model.addAttribute("poFirm",poFirm);

        List<SysProvince> poFirmList= sysProvinceService.list();
        model.addAttribute("poFirmList",poFirmList);
        model.addAttribute("cUser",getSysUser());

        return prefix+"edit";
    }

    @Log(title = "品牌修改", businessType = BusinessType.UPDATE)
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResult editSave(@Validated PoFirm poFirm){
        return toAjax(poFirmService.updateById(poFirm));
    }

    @RequiresPermissions("firm:manage:remove")
    @Log(title = "品牌删除", businessType = BusinessType.DELETE)
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids){
        return toAjax(poFirmService.removeById(ids));
    }

}
